<?php
namespace App\Admin\Controllers\Products;

use App\Admin\Actions\Products\AmazonSkuCCS\ImportAmazonSkuCCS;
use App\Exporter\Products\AmazonSkuCCSExporter;
use App\Models\Basics\AmazonCountry;
use App\Models\Product\ProductAmazonSkuCCS;
use App\Models\Product\ProductClassify;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Form;
class AmazonSkuCCSController extends AdminController{

    protected $title = 'Amazon销售管理';

    protected function grid()
    {
        $grid = new Grid(new ProductAmazonSkuCCS());
        $grid->column('country.country','国家');
        $grid->column('classify.season','季节性')->using(ProductClassify::$_season)->style('min-width:50px');
        $grid->column('classify.title','品类')->style('max-width:200px');
        $grid->column('seller_id','销售员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('classify.status','品类状态')->bool([1 => true, 2 => false])->style('max-width:200px');
        $grid->exporter(new AmazonSkuCCSExporter());
        $grid->fixHeader();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportAmazonSkuCCS());
        });
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter){
                $filter->equal('country_id','国家')->select(AmazonCountry::options());
                $filter->equal('classify_id', '品类')->select(ProductClassify::options());
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
                $filter->equal('seller_id','销售员')->select(User::getSellerOptions(FALSE));
            });
        });

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new ProductAmazonSkuCCS());

        $form->tools(function (Form\Tools $tools){
            $tools->disableView();
            $tools->disableDelete();
        });

        $form->hidden('id');
        $form->select('country_id', '国家')
            ->options(AmazonCountry::options())
            ->required()
            ->setWidth(5);
        $form->select('classify_id', '品类')
            ->options(ProductClassify::options())
            ->required()
            ->setWidth(5);
        $form->select('seller_id','销售员')->options(User::getSellerOptions(False))
            ->default(Admin::user()->id)->rules('required')->required()->setWidth(5);

        if($form->isCreating()){
            $form->disableCreatingCheck(false);
        }
        $form->saving(function (Form $form) {
            // 判断是否是新增操作
            if ($form->isCreating()) {
                $count = ProductAmazonSkuCCS::query()
                    ->where('country_id',$form->country_id)
                    ->where('classify_id',$form->classify_id)
                    ->count();
                if($count > 0){
                    admin_error('重复添加','该品类+国家已存在');
                    return back()->withInput();
                }
            }else{
                $count = ProductAmazonSkuCCS::query()
                    ->where('id','<>',$form->model()->id)
                    ->where('country_id',$form->country_id)
                    ->where('classify_id',$form->classify_id)
                    ->count();
                if($count > 0){
                    admin_error('重复添加','该品类+国家已存在');
                    return back()->withInput();
                }
            }
        });
        $form->saved(function (Form $form) {
            ProductAmazonSkuCCS::upSellerId($form->model()->country_id,$form->model()->classify_id,$form->model()->seller_id);
        });
        return $form;
    }
}
